package com.ponhu.ea2.goodsCenter.provider.facade.impl;

import com.ponhu.ea2.enums.ResponseCodeEnum;
import com.ponhu.ea2.goodsCenter.provider.biz.PonHuGoodsService;
import com.ponhu.ea2.goodsCenter.provider.mapper.PonHuGoodsTradePriceInfoMapper;
import com.ponhu.ea2.service.facade.SellingPointsService;
import com.ponhu.ea2.support.exception.BusinessRuntimeException;
import com.ponhu.ea2.vo.PonHuGoodsTradePriceInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;

/**
 * @author luyu
 * @create 2021-11-22 14:11
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = SellingPointsService.class, timeout = 6000)
public class SellingPointsServiceImpl implements SellingPointsService {

    @Resource
    PonHuGoodsTradePriceInfoMapper ponHuGoodsTradePriceInfoMapper;

    @Resource
    PonHuGoodsService ponHuGoodsService;

    @Override
    public boolean updateSellingPointsAudioByInventoryCode(String inventoryCode) {
        log.info("更新胖总管商品卖点语音，请求编码为：{}", inventoryCode);
        String process = null;
        try {
            // 获取胖总管的商品卖点
            PonHuGoodsTradePriceInfoVo sellingByCode =
                    ponHuGoodsTradePriceInfoMapper.getGoodsSellingByCode(inventoryCode);
            if (null == sellingByCode) {
                log.error("商品编码：{},未查询到胖总管商品信息，请查看原因", inventoryCode);

                throw new BusinessRuntimeException(ResponseCodeEnum.PONHU_GOODS_NOT_EXISTS_800501);
            }
            // 获取语音的url
            process = ponHuGoodsService.processSellingPointAudioUrl(sellingByCode.getGoodsSelling());
            // 更新商品卖点
            if (StringUtils.isNotBlank(process)) {
                ponHuGoodsTradePriceInfoMapper.updateGoodsSellingAudioUrl(inventoryCode, process);
                log.info("更新{}成功，新卖点地址为：{}", inventoryCode, process);
                return Boolean.TRUE;
            }
        } catch (Exception e) {
            log.error("更新胖总管商品卖点异常：{}", e);
            throw e;
        }
        return Boolean.FALSE;
    }
}
